const NIM = require('./NIM_Web_Chatroom_v5.0.0.js');
const glob = getApp().globalData;
class Chatroom {
    constructor(headers) {
        glob.nim = NIM.getInstance({
            debug: false,
            transports: ['websocket'],
            ntServerAddress: null,
            // 初始化SDk
            appKey: glob.NIMappkey,
            account: headers.account,
            token: headers.token,
            chatroomId: headers.roomId,
            chatroomAddresses: headers.roomAddr,
            onconnect:headers.onconnect,
            onerror: headers.ononerror,
            onmsgs:headers.onmsgs,
            ondisconnect:(err)=>{
                console.log(err)
                glob.nim.destroy();
                glob.nim = null;
                if (err) {
                    switch (err.code) {
                        // 账号或者密码错误, 请跳转到登录页面并提示错误
                        case 302:
                        wx.showToast({
                            title: '登录状态过期，请重新登录',
                            icon:"none"
                        })
                        wx.clearStorageSync();
                        wx.redirectTo({
                            url: '/pages/views/login/login',
                        })
                        break;
                    }
                }
                if (typeof headers.disconnect == 'function'){
                    headers.disconnect();
                } 
            }
        })
        glob.NIM = NIM;

    }
}

module.exports={
    Chatroom: Chatroom 
}
